﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Windows;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Linq;

namespace PrsmMvvm.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public DelegateCommand<Window> btn_Close { get; set; }
        public DelegateCommand btn_Next { get; set; }

        private string m_strContext;
        public string strContext
        {
            get { return m_strContext; }
            set
            {
                m_strContext = value;
                RaisePropertyChanged("strContext");
            }
        }

        private SeriesCollection _SeriesCollectionI;
        public SeriesCollection SeriesCollectionI
        {
            get { return _SeriesCollectionI; }
            set
            {
                if (_SeriesCollectionI != value)
                {
                    _SeriesCollectionI = value;
                    RaisePropertyChanged("SeriesCollectionI");
                }
            }
        }

        private SeriesCollection _SeriesCollection;
        public SeriesCollection SeriesCollection
        {
            get { return _SeriesCollection; }
            set
            {
                if (_SeriesCollection != value)
                {
                    _SeriesCollection = value;
                    RaisePropertyChanged("SeriesCollection");
                }
            }
        }

        private List<string> _Labels;
        public List<string> Labels
        {
            get { return _Labels; }
            set
            {
                if (_Labels != value)
                {
                    _Labels = value;
                    RaisePropertyChanged("Labels");
                }
            }
        }

        private List<string> _Labels1;
        public List<string> Labels1
        {
            get { return _Labels1; }
            set
            {
                if (_Labels1 != value)
                {
                    _Labels1 = value;
                    RaisePropertyChanged("Labels1");
                }
            }
        }

        private string text;
        public string Text
        {
            get { return text; }
            set
            {
                if (text != value)
                {
                    text = value;
                    RaisePropertyChanged("Text");
                }
            }
        }
        #region 最下方水平圆柱体
        private SeriesCollection _SeriesCollectionD;
        public SeriesCollection SeriesCollectionD
        {
            get { return _SeriesCollectionD; }
            set
            {
                if (_SeriesCollectionD != value)
                {
                    _SeriesCollectionD = value;
                    RaisePropertyChanged("SeriesCollectionD");
                }
            }
        }

        private string[] _LabelsD;
        public string[] LabelsD
        {
            get { return _LabelsD; }
            set
            {
                if (_LabelsD != value)
                {
                    _LabelsD = value;
                    RaisePropertyChanged("LabelsD");
                }
            }
        }
        #endregion


        #region 左侧饼图
        private SeriesCollection _SeriesCollectionL;
        public SeriesCollection SeriesCollectionL
        {
            get { return _SeriesCollectionL; }
            set
            {
                if (_SeriesCollectionL != value)
                {
                    _SeriesCollectionL = value;
                    RaisePropertyChanged("SeriesCollectionL");
                }
            }
        }
        #endregion

        #region 右侧饼图
        private SeriesCollection _SeriesCollectionR;
        public SeriesCollection SeriesCollectionR
        {
            get { return _SeriesCollectionR; }
            set
            {
                if (_SeriesCollectionR != value)
                {
                    _SeriesCollectionR = value;
                    RaisePropertyChanged("SeriesCollectionR");
                }
            }
        }
        #endregion

        private void btnClose(Window window)
        {
            if (window != null)
            {
                window.Close();
            }
        }


        private void btnNext()
        {
            Views.SeWindow window = new Views.SeWindow();
            window.Show();
        }

        public MainWindowViewModel()
        {
            btn_Close = new DelegateCommand<Window>(btnClose);
            btn_Next = new DelegateCommand(btnNext);

            strContext = "报表分析系统";
            PiestartL();
            PiestartR();
            StackedRow();
            linestart();
        }

        public void PiestartR()
        {
            SeriesCollectionR = new SeriesCollection
            {

            new PieSeries
                            {
                                Title = "Chrome",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
            new PieSeries
                            {
                                Title = "Mozilla",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
             new PieSeries
                            {
                                Title = "Opera",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
             new PieSeries
                            {
                                Title = "Explorer",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            }
             };
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    //通过Dispatcher在工作线程中更新窗体的UI元素
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        var r = new Random();
                        var c = SeriesCollectionR.Count > 0 ? SeriesCollectionR[0].Values.Count : 5;

                        var vals = new ChartValues<ObservableValue>();
                        for (var i = 0; i < c; i++)
                        {
                            vals.Add(new ObservableValue(r.Next(1, 10)));
                        }
                        SeriesCollectionR.Add(new PieSeries
                        {
                            Title = r.ToString(),
                            Values = vals,
                            DataLabels = true
                        });
                        //更新横坐标
                        SeriesCollectionR.RemoveAt(0);
                    });
                }

            });
        }

        public void StackedRow()
        {
            double _trend;
            double _trend1;
            SeriesCollectionD = new SeriesCollection
            {
                new RowSeries
                {
                    Title = "2015",
                    Values = new ChartValues<double> {  39, 50 }
                }
            };

            SeriesCollectionD.Add(new RowSeries
            {
                Title = "2016",
                Values = new ChartValues<double> { 42 }
            });

            SeriesCollectionD[1].Values.Add(48d);

            string[] Labels = new[] { "人工", "自动" };

            Func<double, string> Formatter = value => value.ToString("N");

            Task.Run(() =>
            {
                var r = new Random();
                while (true)
                {
                    Thread.Sleep(1000);
                    _trend = r.Next(0, 100);
                    _trend1 = r.Next(0, 100);

                    //通过Dispatcher在工作线程中更新窗体的UI元素
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        //更新横坐标
                        SeriesCollectionD[0].Values.Add(_trend);
                        SeriesCollectionD[1].Values.Add(_trend1);
                        SeriesCollectionD[0].Values.RemoveAt(0);
                        SeriesCollectionD[1].Values.RemoveAt(0);
                        LabelsD = Labels;
                    });
                }
            });
        }

        public void linestart()
        {
            List<string> Labels;
            List<string> Labels1;
            double _trend;
            double _trend1;

            CartesianChart.Colors = new List<System.Windows.Media.Color>
            {
             System.Windows.Media.Colors.Red,
             System.Windows.Media.Colors.Blue,
             System.Windows.Media.Colors.Green
            };
            //实例化一条折线图   
            LineSeries mylineseries = new LineSeries();
            LineSeries mylineseries1 = new LineSeries();

            //设置折线的标题
            mylineseries.Title = "生产";
            mylineseries1.Title = "消耗";
            //折线图直线形式
            mylineseries.LineSmoothness = 0;
            mylineseries1.LineSmoothness = 0;
            //折线图的无点样式
            mylineseries.PointGeometry = null;
            mylineseries1.PointGeometry = null;
            //添加横坐标
            int[] arr = Enumerable.Range(1, 100).ToArray();
            Labels = new List<string>(arr.Select(i => i.ToString()).ToArray());
            Labels1 = new List<string>(arr.Select(i => i.ToString()).ToArray());
            double[] temp = { 1, 3, 2, 4, -3, 5, 2, 1 };
            //添加折线图的数据
            mylineseries.Values = new ChartValues<double>(temp);
            mylineseries1.Values = new ChartValues<double>(temp);
            SeriesCollectionI = new SeriesCollection { };
            SeriesCollectionI.Add(mylineseries);
            SeriesCollectionI.Add(mylineseries1);
            _trend = 8;
            _trend1 = 8;

            Task.Run(() =>
            {
                var r = new Random();
                while (true)
                {
                    Thread.Sleep(1000);
                    _trend = r.Next(-12, 12);
                    _trend1 = r.Next(-12, 12);

                    //通过Dispatcher在工作线程中更新窗体的UI元素
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        //更新横坐标时间
                        Labels.Add(DateTime.Now.ToString());
                        Labels.RemoveAt(0);
                        //更新纵坐标数据
                        SeriesCollectionI[0].Values.Add(_trend);
                        SeriesCollectionI[1].Values.Add(_trend1);
                        SeriesCollectionI[0].Values.RemoveAt(0);
                        SeriesCollectionI[1].Values.RemoveAt(0);
                    });
                }
            });
        }

        public void PiestartL()
        {
            SeriesCollectionL = new SeriesCollection
            {

            new PieSeries
                            {
                                Title = "Chrome",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
            new PieSeries
                            {
                                Title = "Mozilla",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
             new PieSeries
                            {
                                Title = "Opera",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            },
             new PieSeries
                            {
                                Title = "Explorer",
                                Values = new ChartValues<ObservableValue> { new ObservableValue(new Random().Next(1, 10)) },
                                DataLabels = true
                            }
             };
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        var r = new Random();
                        var c = SeriesCollectionL.Count > 0 ? SeriesCollectionL[0].Values.Count : 5;

                        var vals = new ChartValues<ObservableValue>();
                        for (var i = 0; i < c; i++)
                        {
                            vals.Add(new ObservableValue(r.Next(1, 10)));
                        }
                        SeriesCollectionL.Add(new PieSeries
                        {
                            Title = r.ToString(),
                            Values = vals,
                            DataLabels = true
                        });
                        //更新横坐标
                        SeriesCollectionL.RemoveAt(0);
                    });
                }
            });
        }
    }

}
